#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int h[N], e[N], ne[N], idx;
int w[N], d[N];
LL f[N];
int n;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u)
{
	f[u] = w[u];
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		dfs(j);
		if (f[j] >= 0) {
			f[u] += f[j];
		}
	}
}
int main()
{
	cin >> n;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++)cin >> w[i];
	for (int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		add(b, a);
		d[a]++;
	}
	int root = 1;
	for (int i = 1; i <= n; i++) {
		if (!d[i]) {
			root = i;
			break;
		}
	}
	dfs(root);
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		//cout << "ii==" << i << "    " << f[i] << endl;
		ans = max(ans, f[i]);
	}
	cout << ans << endl;
	return 0;
}